﻿Imports System.Data.SqlClient
Imports BUS
Imports DTO

Public Class frmNhanVien

    Private da As SqlDataAdapter
    Private thaotac As String = "sua"
    Private nv_old As New NhanVienDto

    Private Sub EnableText(ByVal b As Boolean)
        txtmanv.Enabled = b
        txttennv.Enabled = b
        txtquequan.Enabled = b
        txtdantoc.Enabled = b
        txtcmnd.Enabled = b
        txtnoicapcmt.Enabled = b
        txttrinhdovh.Enabled = b
        txtdiachi.Enabled = b
        txtdienthoai.Enabled = b
        txttongiao.Enabled = b
        rdbnam.Enabled = b
        rdbnu.Enabled = b
        dtngaysinh.Enabled = b
        dtngaycapcmt.Enabled = b
        dtngayvaolam.Enabled = b
        cbobophan.Enabled = b
        cbochucvu.Enabled = b
    End Sub

    Private Sub XoaText()
        txtmanv.Text = ""
        txttennv.Text = ""
        txtquequan.Text = ""
        txtdantoc.Text = ""
        txtcmnd.Text = ""
        txtnoicapcmt.Text = ""
        txttrinhdovh.Text = ""
        txtdiachi.Text = ""
        txtdienthoai.Text = ""
        txttongiao.Text = ""
        rdbnam.Checked = False
        rdbnu.Checked = False
        dtngaysinh.Value = Date.Now
        dtngaycapcmt.Value = Date.Now
        dtngayvaolam.Value = Date.Now
        cbobophan.SelectedIndex = 0
        cbochucvu.SelectedIndex = 0

    End Sub

    Private Sub GanText(ByVal index As Integer)
        EnableText(False)
        dgvthongtinnv.Rows(index).Selected = True
        dgvthongtinnv.CurrentCell = dgvthongtinnv.Rows(index).Cells(0)
        txtmanv.Text = dgvthongtinnv.Rows(index).Cells(1).Value
        txttennv.Text = dgvthongtinnv.Rows(index).Cells(2).Value
        rdbnam.Checked = dgvthongtinnv.Rows(index).Cells(3).Value
        rdbnu.Checked = dgvthongtinnv.Rows(index).Cells(4).Value
        dtngaysinh.Value = dgvthongtinnv.Rows(index).Cells(5).Value
        txtquequan.Text = dgvthongtinnv.Rows(index).Cells(6).Value
        txtdantoc.Text = dgvthongtinnv.Rows(index).Cells(7).Value
        txtcmnd.Text = dgvthongtinnv.Rows(index).Cells(8).Value
        dtngaycapcmt.Value = dgvthongtinnv.Rows(index).Cells(9).Value
        txtnoicapcmt.Text = dgvthongtinnv.Rows(index).Cells(10).Value
        txttrinhdovh.Text = dgvthongtinnv.Rows(index).Cells(11).Value
        txtdiachi.Text = dgvthongtinnv.Rows(index).Cells(12).Value
        txtdienthoai.Text = dgvthongtinnv.Rows(index).Cells(13).Value
        dtngayvaolam.Value = dgvthongtinnv.Rows(index).Cells(14).Value
        cbobophan.SelectedValue = dgvthongtinnv.Rows(index).Cells(15).Value
        cbochucvu.SelectedValue = dgvthongtinnv.Rows(index).Cells(16).Value
        txttongiao.Text = dgvthongtinnv.Rows(index).Cells(17).Value
    End Sub

    Private Sub LoadData()
        Dim dt As New DataTable
        da = NhanVienBus.LayDSDanhSach()
        da.Fill(dt)
        dgvthongtinnv.DataSource = dt
    End Sub

    Private Sub txttimkiem_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttimkiem.Enter
        txttimkiem.Text = ""
    End Sub

    Private Sub txttimkiem_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttimkiem.Leave
        If txttimkiem.Text = "" Or txttimkiem.Text = "Mã nhân viên" Then
            txttimkiem.Text = "Mã nhân viên"
        End If
    End Sub

    Private Sub frmNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbobophan.DataSource = BoPhanBus.LayDanhSach
        cbobophan.DisplayMember = "tenbp"
        cbobophan.ValueMember = "mabp"

        cbochucvu.DataSource = ChucVuBus.LayDanhSach
        cbochucvu.DisplayMember = "tencv"
        cbochucvu.ValueMember = "macv"

        cbobophan.SelectedIndex = 0
        cbochucvu.SelectedIndex = 0
        LoadData()
        If dgvthongtinnv.RowCount > 0 Then
            GanText(0)
        End If
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        EnableText(True)
        XoaText()
        txtmanv.Focus()
        thaotac = "them"
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Dim nv As New NhanVienDto
        nv.MaNV = txtmanv.Text
        nv.TenNV = txttennv.Text
        nv.GioiTinh = If(rdbnam.Checked = True, True, False)
        nv.NgaySinh = Convert.ToDateTime(String.Format("{0:yyyy-MM-dd}", dtngaysinh.Value))
        nv.QueQuan = txtquequan.Text
        nv.DanToc = txtdantoc.Text
        nv.CMND = txtcmnd.Text
        nv.NgayCap = Convert.ToDateTime(String.Format("{0:yyyy-MM-dd}", dtngaycapcmt.Value))
        nv.NoiCap = txtnoicapcmt.Text
        nv.TrinhDo = txttrinhdovh.Text
        nv.DiaChi = txtdiachi.Text
        nv.DienThoai = txtdienthoai.Text
        nv.NgayVaoLam = Convert.ToDateTime(String.Format("{0:yyyy-MM-dd}", dtngayvaolam.Value))
        nv.MaBP = cbobophan.SelectedValue
        nv.MaCV = cbochucvu.SelectedValue
        nv.TonGiao = txttongiao.Text
        Dim vt As Integer = -1

        If thaotac = "them" Then
            Try
                If txtmanv.Text = "" And txttennv.Text = "" Then
                    MessageBox.Show("Vui long nhap ma nhan vien va ten nhan vien")
                    Return
                End If

                NhanVienBus.ThemNV(nv)

                LoadData()
                For i As Integer = 0 To dgvthongtinnv.RowCount - 1 Step 1
                    If dgvthongtinnv.Rows(i).Cells(1).Value = txtmanv.Text Then
                        vt = i
                        Exit For
                    End If
                Next
                GanText(vt)
                txtmanv.Enabled = False
                thaotac = "sua"
                MessageBox.Show("Da them nhan vien")

            Catch 'ex As Exception
                MessageBox.Show("Loi trong qua trinh nhap hay trung du lieu")
            End Try
        Else
            NhanVienBus.SuaNV(nv)
            For i As Integer = 0 To dgvthongtinnv.RowCount - 1 Step 1
                If dgvthongtinnv.Rows(i).Cells(1).Value = txtmanv.Text Then
                    vt = i
                    Exit For
                End If
            Next
            LoadData()
            GanText(vt)
            MessageBox.Show("Da sua nhan vien")
        End If


    End Sub

    Private Sub dgvthongtinnv_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvthongtinnv.CellClick
        If e.RowIndex < 0 Or e.ColumnIndex < 0 Then
            Return
        End If

        GanText(e.RowIndex)
    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        If txtmanv.Text = "" Then
            Return
        End If

        Dim res As MsgBoxResult = MsgBox("Ban co muon xoa", MsgBoxStyle.YesNo, "Thong bao")
        Select Case res
            Case MsgBoxResult.Yes
                NhanVienBus.XoaNV(txtmanv.Text)

                For i As Integer = 0 To dgvthongtinnv.Rows.Count - 1 Step 1
                    If txtmanv.Text = dgvthongtinnv.Rows(i).Cells(1).Value.ToString() Then
                        dgvthongtinnv.Rows.RemoveAt(i)
                        Exit For
                    End If
                Next
                LoadData()
                If dgvthongtinnv.Rows.Count > 0 Then
                    GanText(0)
                Else
                    XoaText()
                End If

                MessageBox.Show("Da xoa nhan vien")

            Case MsgBoxResult.No
                Return
        End Select

    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        EnableText(True)
        txtmanv.Enabled = False
        thaotac = "sua"

    End Sub

    Private Sub btntimkiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntimkiem.Click
        Dim flg As Boolean = False
        For i As Integer = 0 To dgvthongtinnv.Rows.Count - 1 Step 1
            If txttimkiem.Text = dgvthongtinnv.Rows(i).Cells(1).Value.ToString() Then
                dgvthongtinnv.Rows(i).Selected = True
                GanText(i)
                flg = True
                Exit For
            End If
        Next

        If flg = False Then
            MessageBox.Show("Khong co nhan vien nay")
        End If

    End Sub

    Private Sub txttimkiem_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txttimkiem.KeyPress
        Dim mK As Integer = Asc(e.KeyChar)
        If mK >= 97 And mK <= 122 Then
            e.KeyChar = e.KeyChar.ToString.ToUpper
        End If
    End Sub

End Class